package com.situ.day17;

import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.junit.Test;

public class JDBCDemo {

	@Test
	public void testJDBC() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		
		try {
//			1、加载驱动Class.forName("");
			Class.forName("com.mysql.jdbc.Driver");
//			2、获得连接对象Connection
			 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java1907?useUnicode=true&characterEncoding=UTF-8","root", "1234");
//			3、写sql语句
			String sql = "select id,`name`,age,gender from student";
//			4、创建Statement
			 statement = connection.createStatement();
//			5、执行语句
//		    (1)、更新类（只要改变表里面的数据） ：delete、update、insert                    executeUpdate()
//		            返回值：int    表示影响的行数
//		    (2)、查询类（没有改变表里面的数据） ：select                                                executeQuery()
//		            返回值：结果集ResultSet
			 resultSet = statement.executeQuery(sql);
			ArrayList<Student> list = new ArrayList<Student>();
			// 判断下一行有没有，如果有返回true，指向下一行，如果没有返回false
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name= resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
//			6、关闭连接
			if ( resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testDelete() {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			statement = connection.createStatement();
			String sql = "delete from student where id = 2";
			int count = statement.executeUpdate(sql);
			if (count == 1) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, statement, null);
		}
	}
	
	@Test
	public void testPrepareStatement() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 1);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	
	@Test
	public void testPrepareStatement2() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO student(id,`name`,age,gender) VALUES(?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 4);
			preparedStatement.setString(2, "赵六2");
			preparedStatement.setInt(3, 22);
			preparedStatement.setString(4, "男");
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			}else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
	}
	
	@Test
	public void testPrepareStatement3() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set age = ? where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, 34);
			preparedStatement.setInt(2, 2);
			int count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("更改成功");
			}else {
				System.out.println("更改失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
	
	@Test
	public void testJDBC4() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql1 = "UPDATE account SET money = money-10000 WHERE `name`='张三'";
			String sql2 = "UPDATE account SET money = money+10000 WHERE `name`='李四'";
			connection.setAutoCommit(false);
			preparedStatement = connection.prepareStatement(sql1);
			preparedStatement.executeUpdate();
			preparedStatement = connection.prepareStatement(sql2);
			preparedStatement.executeUpdate();
			connection.commit();
		} catch (SQLException e) {
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
}
